package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.mapper.ItemMapper;
import com.neuedu.pojo.Item;
import com.neuedu.service.IItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author han
 * @since 2020-09-21
 */
@Service
public class ItemServiceImpl extends ServiceImpl<ItemMapper, Item> implements IItemService {

    @Override
    public Object list(Item item) {
        // 如果 有name传过来 就按照name 模糊查询
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        System.out.println(item.getTypeId()+" "+item.getPrincipalId()+" "+item.getBeginTime()+" "+item.getEndTime());
        if(StringUtils.isNotBlank(item.getItemName())) {
            wrapper.like("item_name", item.getItemName());
        }
        if(item.getTypeId()!=null) {
            wrapper.like("i.type_id", item.getTypeId());
        }
        if(item.getPrincipalId()!=null) {
            wrapper.like("i.principal_id", item.getPrincipalId());
        }
        if(StringUtils.isNotBlank(item.getBeginTime())&&StringUtils.isNotBlank(item.getEndTime())){
            wrapper.between("build_time",item.getBeginTime(),item.getEndTime());
        }

        wrapper.orderByAsc("id");
        // 如果分页返回 IPage 如果不分页 返回 List
        if(item.getWithPage() == 1) {
            //return this.page(new Page<>(item.getPageNo(),item.getPageSize()),wrapper);
            return getBaseMapper().list(new Page<>(item.getPageNo(),item.getPageSize()),wrapper);
        } else {
            return this.list(wrapper);
        }
    }

    @Override
    public boolean add(Item item) {
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        wrapper.eq("item_name",item.getItemName());
        Item query = this.getOne(wrapper);
        if(query == null )
            return this.save(item);
        return false;
    }

    @Override
    public boolean batchdel(Integer[] ids) {
        List<Integer> list = new ArrayList<>(ids.length);
        Collections.addAll(list,ids);
        Item item = new Item();
        item.setActive(0);
        UpdateWrapper<Item> wrapper = new UpdateWrapper<>();
        wrapper.in("id",list);
        return this.update(item,wrapper);
    }




}
